package DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;

import XML.XML;
import XML.XMLNode;

public class DBHandler {

	public static boolean AddRes(String Md5, String Type, String Title, Long length, Integer Pipsum) {
		String sql = "INSERT INTO resInfo(Md5,Type,Title,Length,Pipsum)VALUES(?,?,?,?,?)";
		Vector al = new Vector();
		al.add(Md5);
		al.add(Type);
		al.add(Title);
		al.add(length);
		al.add(Pipsum);
		return CNN.executeUpdata(sql, al) == 1;
	}

	/**
	 * 查询用户是否存在
	 * 
	 * @param id
	 *            用户名
	 * @param pass
	 *            密码，null表示不检测密码
	 * @return 存在状态
	 */
	public static boolean existUser(String id, String pass) {
		String sql = "SELECT * FROM users WHERE id=? " + (pass != null ? "and pwd=?" : "");
		boolean b = false;
		Vector al = new Vector();
		al.add(id);
		if (pass != null)
			al.add(pass);
		ResultSet rs = CNN.executeQuery(sql, al);
		try {
			b = rs != null ? rs.next() : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}

	/**
	 * 取好友名单
	 * 
	 * @param UserID
	 * @return
	 */
	public static String[] getFellowID(String UserID) {
		String sql = "SELECT fellowID FROM fellow WHERE Id=?";
		Vector<String> al = new Vector<String>();
		al.add(UserID);
		ResultSet rs = CNN.executeQuery(sql, al);
		al.clear();
		try {
			while (rs.next()) {
				al.add(rs.getString(1));
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		Object[] fl = al.toArray();
		String[] yh = new String[fl.length];
		for (int i = 0; i < yh.length; i++)
			yh[i] = fl[i].toString();
		return yh;
	}

	/**
	 * 取群用户列表
	 * 
	 * @param groupId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String[] getGroupUser(int groupId) {
		String sql = "SELECT Id FROM Group_member WHERE GroupID=?";
		Vector al = new Vector();
		al.add(new Integer(groupId));
		ResultSet rs = CNN.executeQuery(sql, al);
		al.clear();
		try {
			while (rs.next()) {
				al.add(rs.getString(1));
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		Object[] fl = al.toArray();
		String[] yh = new String[fl.length];
		for (int i = 0; i < yh.length; i++)
			yh[i] = fl[i].toString();
		return yh;
	}

	/**
	 * 保存离线消息
	 * 
	 * @param uid
	 *            用户名
	 * @param msg
	 *            消息指令内容
	 */
	public static boolean putLeaveMsg(String uid, String msg) {
//		boolean b = existUser(uid, null);
		String sql = "INSERT INTO serv_leaveOnlineMsg(userId,leaveMsg,[Time])VALUES(?,?,default)";
		Vector al = new Vector();
		al.add(uid);
		al.add(msg);
		return CNN.executeUpdata(sql, al) == 1;
	}

	public static void putLeaveMsg(String uid, XML msg) {
		putLeaveMsg(uid, msg.createCode(false, ""));
	}

	public static void putLeaveMsg(String uid, XMLNode msg) {
		putLeaveMsg(uid, msg.toString());
	}

	public static boolean resContribute(String UserID) {
		String sql = "UPDATE users SET integral=integral+1 WHERE id=?";
		Vector al = new Vector();
		al.add(UserID);
		return CNN.executeUpdata(sql, al) == 1;
	}

	public static boolean resExists(String Md5) {
		String sql = "SELECT * FROM resInfo WHERE MD5=?";
		Vector al = new Vector();
		al.add(Md5);
		try {
			return CNN.executeQuery(sql, al).next();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 设置上线IP和上线时间
	 * 
	 * @param address
	 * @return
	 */
	public static boolean setOlineIp(String userID, String IPaddress) {
		String sql = "UPDATE users set onlineIP=?,LatelyOnline=? WHERE Id=?";
		Vector al = new Vector();
		al.add(IPaddress);
		al.add(new java.sql.Date(new Date().getTime()));
		al.add(userID);
		int i = CNN.executeUpdata(sql, al);
		return i > 0;
	}

	public UserInfo getUser(String id) {
		String sql = "SELECT * FROM users WHERE id=?";
		Vector al = new Vector();
		UserInfo user = null;
		al.add(id);
		ResultSet rs = CNN.executeQuery(sql, al);
		try {
			while (rs.next()) {
				user = new UserInfo(rs.getString("id"), rs.getInt("integral"), rs.getInt("gradeID"), rs.getLong("online_time"));
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return user;
	}

	/**
	 * 判断是否加入指定群
	 * 
	 * @param ID
	 *            群编号
	 * @param userID
	 *            用户名
	 * @return
	 */
	public boolean groupIn(int id, String userID) {
		String sql = "SELECT * FROM Group_member WHERE GroupID=? and Id=?";
		Vector al = new Vector();
		al.add(new Integer(id));
		al.add(userID);
		ResultSet rs = CNN.executeQuery(sql, al);
		boolean b = false;
		try {
			b = rs.next();
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return b;
	}
}
